package com.qzsf.backend.mapper;

import com.qzsf.backend.dto.UserDetailDTO;
import com.qzsf.backend.entity.UserProfile;
import com.qzsf.backend.vo.MobileUserProfileVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/*
 * @Author: Dennis Chen
 * @CreateTime: 2024/10/31 15:21 周四
 * @Email 1429278462@qq.com
 * @Remark 用户详细信息数据访问层
 * @Version 1.0
 */
@Mapper
@Repository
public interface UserProfileMapper {
    @Select("select a.id,a.status,b.name,b.pic,b.sex,b.phone,b.email,b.address,b.student_id,b.enrollment_year,b.faculty from userauth a left join user_profile b on a.id = b.id where role = 'USER'")
    List<UserDetailDTO> getAllUserDetail();

    @Select("select a.id,a.status,b.name,b.pic,b.sex,b.phone,b.email,b.address,b.student_id,b.enrollment_year,b.faculty from userauth a left join user_profile b on a.id = b.id where role = 'USER' and b.name like concat('%',#{name},'%') and b.enrollment_year like concat('%',#{enrollmentYear},'%') and faculty like concat('%',#{faculty},'%')")
    List<UserDetailDTO> getAllUserDetailByNameOrYear(String name, String enrollmentYear, String faculty);

    @Insert("insert into user_profile (id, name, pic, sex, phone, email, address, student_id, enrollment_year, faculty) values (#{id}, #{name}, #{pic}, #{sex}, #{phone}, #{email}, #{address}, #{studentId}, #{enrollmentYear}, #{faculty})")
    int createUserProfile(UserProfile userProfile);

    @Update("update user_profile set  name = #{name},pic = #{pic},sex = #{sex},phone = #{phone},email = #{email},address = #{address},student_id = #{studentId},enrollment_year = #{enrollmentYear},faculty = #{faculty}  where id = #{id};")
    int updateUserProfile(UserProfile userProfile);
    
    @Select("select * from user_profile where id = #{id}")
    UserProfile getUserProfileById(int id);

    @Select("select a.name,a.pic,sum(b.hours) as hours,a.student_id,a.faculty  from user_profile a left join participation b on a.id = b.user_id where a.id = #{id} group by a.id")
    MobileUserProfileVo getMobileUserProfile(int id);
}
